package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.ui.view.MoreGridview;
import com.joplus.housekeeper.utils.CalendarUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zisheng on 2017/9/19.
 */

public class ChatCalendarAdapter extends BaseAdapter {
    Context context;
    String firstTime;
    String accountId;
    private LayoutInflater layout;
    private Date pastDate;
    Map<String, List<Integer>> timeMap;


    public ChatCalendarAdapter(Context context, String s, Map<String, List<Integer>> timeMap, String accountId) {
        firstTime = s;
        this.context = context;
        layout = LayoutInflater.from(context);
        this.timeMap = timeMap;
        this.accountId = accountId;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            pastDate = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getCount() {
        return CalendarUtils.pastTimeCount(firstTime) + 1;

    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = layout.inflate(R.layout.item_hotel_calendar, null);
            holder = new Holder(convertView);
            convertView.setTag(holder);
        }
        holder = (Holder) convertView.getTag();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(pastDate);
        calendar.add(Calendar.MONTH, position);
        holder.tv.setText(calendar.get(Calendar.YEAR) + context.getString(R.string.year)
                + (calendar.get(Calendar.MONTH) + 1) + context.getString(R.string.month));

       /* String s = calendar.get(Calendar.YEAR) + "-"
                + (calendar.get(Calendar.MONTH) + 1);*/
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(calendar.get(Calendar.YEAR) + "-");
        int monthi = calendar.get(Calendar.MONTH) + 1;
        stringBuffer.append(monthi < 10 ? "0" + monthi : monthi);

        if (timeMap.containsKey(stringBuffer.toString())) {

            holder.gv.setAdapter(new ChatCalendarGvAdapter(context, calendar, timeMap.get(stringBuffer.toString()), accountId));
        } else {
            holder.gv.setAdapter(new ChatCalendarGvAdapter(context, calendar));
        }


        return convertView;
    }

    public static class Holder {
        public Holder(View view) {
            tv = (TextView) view.findViewById(R.id.tv);
            gv = (MoreGridview) view.findViewById(R.id.gv);
        }

        private TextView tv;
        private MoreGridview gv;

    }
}
